package myschool.web;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import myschool.biz.GradeService;
import myschool.biz.StudentService;
import myschool.entity.Grade;
import myschool.entity.Page;
import myschool.entity.Result;
import myschool.entity.Student;
import myschool.util.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//*.d
public class CommonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String path = req.getServletPath();
        path = path.substring(1, path.length() - 3);//
        try {
            Method m = this.getClass().getDeclaredMethod(path, HttpServletResponse.class, HttpServletRequest.class);
            Object r = m.invoke(this, resp, req);
            if (r == null)
                return;
            if (r instanceof String) {
                //认为是转发地址
                req.getRequestDispatcher((String) r).forward(req, resp);
                return;
            }
            //其他情况，发json对象
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().print(JSON.toJSONString(r));
        } catch (Exception e) {
            //writer.println("出现了异常：" + e);
            e.printStackTrace();
        }

    }

    //删除一个学生
    public Result delStu(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int r=1;//，，，，
        return r>1?Result.success():Result.error("无法删除");
    }
        public void login(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("uname");
        String pass = request.getParameter("upass");
        if (name.startsWith("java82") && pass.equals("123456")) {
            HttpSession session = request.getSession();
            session.setAttribute("USER_LOGIN", name);
            response.sendRedirect("main.jsp");
            return;
        }
        response.getWriter().println("login fail");
    }

    public String login2(HttpServletResponse response, HttpServletRequest request) throws ServletException,
            IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("uname");
        String pass = request.getParameter("upass");
        if (name.startsWith("java82") && pass.equals("123456")) {
            HttpSession session = request.getSession();
            session.setAttribute("USER_LOGIN", name);

            return "welcome.jsp";
        }
        return "fail.jsp";
    }

    public List<Grade> grades(HttpServletResponse response, HttpServletRequest request) throws ServletException,
            IOException {
        List<Grade> all = GradeService.ME.findAll();

        return all;
    }

    //查询学生的信息，并且返回ajax的学生分页
    public Page<Student> stuAjax(HttpServletResponse response, HttpServletRequest request) throws ServletException,
            IOException {
        String sname = StrUtil.toStr(request.getParameter("sname"));
        String tel = StrUtil.toStr(request.getParameter("tel"));
        Date start = StrUtil.toDate(request.getParameter("dateStart"));
        Date end = StrUtil.toDate(request.getParameter("dateEnd"));
        int pageNo = StrUtil.toInt(request.getParameter("pageNo"), 1);
        Page<Student> page = StudentService.ME.search(sname, tel, start, end, pageNo);

        return page;
    }
    public void stuAjax2(HttpServletResponse response, HttpServletRequest request) throws ServletException,
            IOException {
        String sname = StrUtil.toStr(request.getParameter("sname"));
        String tel = StrUtil.toStr(request.getParameter("tel"));
        Date start = StrUtil.toDate(request.getParameter("dateStart"));
        Date end = StrUtil.toDate(request.getParameter("dateEnd"));
        int pageNo = StrUtil.toInt(request.getParameter("pageNo"), 1);
        Page<Student> page = StudentService.ME.search(sname, tel, start, end, pageNo);

        response.getWriter().print(JSON.toJSONString(page,"yyyy-MM-dd", JSONWriter.Feature.WriteNullListAsEmpty,JSONWriter.Feature.WriteNullStringAsEmpty));
    }
}
